

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="https://dwj666.cn/%E6%A3%AE%E6%9E%97.png">
  <link rel="icon" href="https://dwj666.cn/%E6%A3%AE%E6%9E%97.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="主要利用刷题来熟悉sql关键字和函数使用">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  <meta name="description" content="主要利用刷题来熟悉sql关键字和函数使用">
<meta property="og:type" content="article">
<meta property="og:title" content="sql刷题-计算用户的平均次日留存率">
<meta property="og:url" content="http://example.com/2023/02/10/sql%E7%AF%87-%E7%AE%97%E6%B3%95%E4%B8%80/index.html">
<meta property="og:site_name" content="藏色的个人博客">
<meta property="og:description" content="主要利用刷题来熟悉sql关键字和函数使用">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://uploadfiles.nowcoder.com/images/20210809/373115_1628511730741/35869B8BF490DC7526BD4B3A99D80A3F">
<meta property="og:image" content="https://uploadfiles.nowcoder.com/images/20211021/999991344_1634818790637/76C646AA09C6F01157BF11A7103C1E7B">
<meta property="article:published_time" content="2023-02-10T03:02:19.935Z">
<meta property="article:modified_time" content="2023-02-10T03:02:20.220Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://uploadfiles.nowcoder.com/images/20210809/373115_1628511730741/35869B8BF490DC7526BD4B3A99D80A3F">
  
  <title>sql刷题-计算用户的平均次日留存率 - 藏色的个人博客</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="/hexo-gujijih/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/hexo-gujijih/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"example.com","root":"/hexo-gujijih/","version":"1.8.12","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname"}},"search_path":"/hexo-gujijih/local-search.xml"};
  </script>
  <script  src="/hexo-gujijih/js/utils.js" ></script>
  <script  src="/hexo-gujijih/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/hexo-gujijih/">
      <strong>藏色的个人博客</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hexo-gujijih/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hexo-gujijih/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hexo-gujijih/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hexo-gujijih/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/hexo-gujijih/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('https://dwj666.cn/dmfj.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="sql刷题-计算用户的平均次日留存率">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2023-02-10 11:02" pubdate>
        2023年2月10日 上午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      3.9k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      12 分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">sql刷题-计算用户的平均次日留存率</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：2023年2月10日 上午
                
              </p>
            
            <div class="markdown-body">
              <p>主要利用刷题来熟悉sql关键字和函数使用</p>
<span id="more"></span>

<h4 id="计算用户的平均次日留存率"><a href="#计算用户的平均次日留存率" class="headerlink" title="计算用户的平均次日留存率"></a><strong>计算用户的平均次日留存率</strong></h4><h5 id="关键字和函数"><a href="#关键字和函数" class="headerlink" title="关键字和函数"></a>关键字和函数</h5><p>关键词 distinct  用于返回唯一不同的值。</p>
<p>DATE_ADD() 函数向日期添加指定的时间间隔。</p>
<h5 id="问题描述"><a href="#问题描述" class="headerlink" title="问题描述"></a>问题描述</h5><p>题目：现在运营想要查看用户在某天刷题后第二天还会再来刷题的平均概率。请你取出相应数据。</p>
<p><img src="https://uploadfiles.nowcoder.com/images/20210809/373115_1628511730741/35869B8BF490DC7526BD4B3A99D80A3F" srcset="/hexo-gujijih/img/loading.gif" lazyload alt="img"></p>
<p>根据示例，你的查询应返回以下结果：</p>
<p><img src="https://uploadfiles.nowcoder.com/images/20211021/999991344_1634818790637/76C646AA09C6F01157BF11A7103C1E7B" srcset="/hexo-gujijih/img/loading.gif" lazyload alt="img"></p>
<h5 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h5><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">drop</span> <span class="hljs-keyword">table</span> if <span class="hljs-keyword">exists</span> `user_profile`;<br><span class="hljs-keyword">drop</span> <span class="hljs-keyword">table</span> if  <span class="hljs-keyword">exists</span> `question_practice_detail`;<br><span class="hljs-keyword">drop</span> <span class="hljs-keyword">table</span> if  <span class="hljs-keyword">exists</span> `question_detail`;<br><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> `user_profile` (<br>`id` <span class="hljs-type">int</span> <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,<br>`device_id` <span class="hljs-type">int</span> <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,<br>`gender` <span class="hljs-type">varchar</span>(<span class="hljs-number">14</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,<br>`age` <span class="hljs-type">int</span> ,<br>`university` <span class="hljs-type">varchar</span>(<span class="hljs-number">32</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,<br>`gpa` <span class="hljs-type">float</span>,<br>`active_days_within_30` <span class="hljs-type">int</span> ,<br>`question_cnt` <span class="hljs-type">int</span> ,<br>`answer_cnt` <span class="hljs-type">int</span> <br>);<br><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> `question_practice_detail` (<br>`id` <span class="hljs-type">int</span> <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,<br>`device_id` <span class="hljs-type">int</span> <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,<br>`question_id`<span class="hljs-type">int</span> <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,<br>`<span class="hljs-keyword">result</span>` <span class="hljs-type">varchar</span>(<span class="hljs-number">32</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,<br>`<span class="hljs-type">date</span>` <span class="hljs-type">date</span> <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span><br>);<br><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> `question_detail` (<br>`id` <span class="hljs-type">int</span> <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,<br>`question_id`<span class="hljs-type">int</span> <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,<br>`difficult_level` <span class="hljs-type">varchar</span>(<span class="hljs-number">32</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span><br>);<br><br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> user_profile <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">1</span>,<span class="hljs-number">2138</span>,<span class="hljs-string">&#x27;male&#x27;</span>,<span class="hljs-number">21</span>,<span class="hljs-string">&#x27;北京大学&#x27;</span>,<span class="hljs-number">3.4</span>,<span class="hljs-number">7</span>,<span class="hljs-number">2</span>,<span class="hljs-number">12</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> user_profile <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">2</span>,<span class="hljs-number">3214</span>,<span class="hljs-string">&#x27;male&#x27;</span>,<span class="hljs-keyword">null</span>,<span class="hljs-string">&#x27;复旦大学&#x27;</span>,<span class="hljs-number">4.0</span>,<span class="hljs-number">15</span>,<span class="hljs-number">5</span>,<span class="hljs-number">25</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> user_profile <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">3</span>,<span class="hljs-number">6543</span>,<span class="hljs-string">&#x27;female&#x27;</span>,<span class="hljs-number">20</span>,<span class="hljs-string">&#x27;北京大学&#x27;</span>,<span class="hljs-number">3.2</span>,<span class="hljs-number">12</span>,<span class="hljs-number">3</span>,<span class="hljs-number">30</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> user_profile <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">4</span>,<span class="hljs-number">2315</span>,<span class="hljs-string">&#x27;female&#x27;</span>,<span class="hljs-number">23</span>,<span class="hljs-string">&#x27;浙江大学&#x27;</span>,<span class="hljs-number">3.6</span>,<span class="hljs-number">5</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> user_profile <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">5</span>,<span class="hljs-number">5432</span>,<span class="hljs-string">&#x27;male&#x27;</span>,<span class="hljs-number">25</span>,<span class="hljs-string">&#x27;山东大学&#x27;</span>,<span class="hljs-number">3.8</span>,<span class="hljs-number">20</span>,<span class="hljs-number">15</span>,<span class="hljs-number">70</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> user_profile <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">6</span>,<span class="hljs-number">2131</span>,<span class="hljs-string">&#x27;male&#x27;</span>,<span class="hljs-number">28</span>,<span class="hljs-string">&#x27;山东大学&#x27;</span>,<span class="hljs-number">3.3</span>,<span class="hljs-number">15</span>,<span class="hljs-number">7</span>,<span class="hljs-number">13</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> user_profile <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">7</span>,<span class="hljs-number">4321</span>,<span class="hljs-string">&#x27;male&#x27;</span>,<span class="hljs-number">28</span>,<span class="hljs-string">&#x27;复旦大学&#x27;</span>,<span class="hljs-number">3.6</span>,<span class="hljs-number">9</span>,<span class="hljs-number">6</span>,<span class="hljs-number">52</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_practice_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">1</span>,<span class="hljs-number">2138</span>,<span class="hljs-number">111</span>,<span class="hljs-string">&#x27;wrong&#x27;</span>,<span class="hljs-string">&#x27;2021-05-03&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_practice_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">2</span>,<span class="hljs-number">3214</span>,<span class="hljs-number">112</span>,<span class="hljs-string">&#x27;wrong&#x27;</span>,<span class="hljs-string">&#x27;2021-05-09&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_practice_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">3</span>,<span class="hljs-number">3214</span>,<span class="hljs-number">113</span>,<span class="hljs-string">&#x27;wrong&#x27;</span>,<span class="hljs-string">&#x27;2021-06-15&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_practice_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">4</span>,<span class="hljs-number">6543</span>,<span class="hljs-number">111</span>,<span class="hljs-string">&#x27;right&#x27;</span>,<span class="hljs-string">&#x27;2021-08-13&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_practice_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">5</span>,<span class="hljs-number">2315</span>,<span class="hljs-number">115</span>,<span class="hljs-string">&#x27;right&#x27;</span>,<span class="hljs-string">&#x27;2021-08-13&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_practice_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">6</span>,<span class="hljs-number">2315</span>,<span class="hljs-number">116</span>,<span class="hljs-string">&#x27;right&#x27;</span>,<span class="hljs-string">&#x27;2021-08-14&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_practice_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">7</span>,<span class="hljs-number">2315</span>,<span class="hljs-number">117</span>,<span class="hljs-string">&#x27;wrong&#x27;</span>,<span class="hljs-string">&#x27;2021-08-15&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_practice_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">8</span>,<span class="hljs-number">3214</span>,<span class="hljs-number">112</span>,<span class="hljs-string">&#x27;wrong&#x27;</span>,<span class="hljs-string">&#x27;2021-05-09&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_practice_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">9</span>,<span class="hljs-number">3214</span>,<span class="hljs-number">113</span>,<span class="hljs-string">&#x27;wrong&#x27;</span>,<span class="hljs-string">&#x27;2021-08-15&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_practice_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">10</span>,<span class="hljs-number">6543</span>,<span class="hljs-number">111</span>,<span class="hljs-string">&#x27;right&#x27;</span>,<span class="hljs-string">&#x27;2021-08-13&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_practice_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">11</span>,<span class="hljs-number">2315</span>,<span class="hljs-number">115</span>,<span class="hljs-string">&#x27;right&#x27;</span>,<span class="hljs-string">&#x27;2021-08-13&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_practice_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">12</span>,<span class="hljs-number">2315</span>,<span class="hljs-number">116</span>,<span class="hljs-string">&#x27;right&#x27;</span>,<span class="hljs-string">&#x27;2021-08-14&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_practice_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">13</span>,<span class="hljs-number">2315</span>,<span class="hljs-number">117</span>,<span class="hljs-string">&#x27;wrong&#x27;</span>,<span class="hljs-string">&#x27;2021-08-15&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_practice_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">14</span>,<span class="hljs-number">3214</span>,<span class="hljs-number">112</span>,<span class="hljs-string">&#x27;wrong&#x27;</span>,<span class="hljs-string">&#x27;2021-08-16&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_practice_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">15</span>,<span class="hljs-number">3214</span>,<span class="hljs-number">113</span>,<span class="hljs-string">&#x27;wrong&#x27;</span>,<span class="hljs-string">&#x27;2021-08-18&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_practice_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">16</span>,<span class="hljs-number">6543</span>,<span class="hljs-number">111</span>,<span class="hljs-string">&#x27;right&#x27;</span>,<span class="hljs-string">&#x27;2021-08-13&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">1</span>,<span class="hljs-number">111</span>,<span class="hljs-string">&#x27;hard&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">2</span>,<span class="hljs-number">112</span>,<span class="hljs-string">&#x27;medium&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">3</span>,<span class="hljs-number">113</span>,<span class="hljs-string">&#x27;easy&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">4</span>,<span class="hljs-number">115</span>,<span class="hljs-string">&#x27;easy&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">5</span>,<span class="hljs-number">116</span>,<span class="hljs-string">&#x27;medium&#x27;</span>);<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> question_detail <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">6</span>,<span class="hljs-number">117</span>,<span class="hljs-string">&#x27;easy&#x27;</span>);<br></code></pre></td></tr></table></figure>

<h5 id="解题"><a href="#解题" class="headerlink" title="解题"></a>解题</h5><h6 id="分析"><a href="#分析" class="headerlink" title="分析"></a>分析</h6><p>关键词1<code>第二天</code>,如下，然后根据用户device_id来判断用户每个用户第二天有没有刷题。</p>
<figure class="highlight reasonml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs reasonml">date<span class="hljs-constructor">_add(<span class="hljs-params">date</span>,<span class="hljs-params">interval</span> 1 <span class="hljs-params">day</span>)</span><br></code></pre></td></tr></table></figure>

<p>关键词2<code>平均概率</code>,如下</p>
<p>通过关键词一，可以确定，当用户第二天打卡，则第二天会有数据，所以以第二天为分子，第一天为分母即可得到平均概率</p>
<h6 id="实施"><a href="#实施" class="headerlink" title="实施"></a>实施</h6><p>步骤一、用于返回每个用户每天唯一的数据</p>
<figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs pgsql"><span class="hljs-keyword">select</span> <span class="hljs-keyword">distinct</span> device_id, <span class="hljs-type">date</span> <span class="hljs-keyword">from</span> question_practice_detail<br></code></pre></td></tr></table></figure>

<p>步骤二、每个用户通过第一天数据和第二天数据做比较，然后使用第二天的数据做分子，第一天的数据做分子得到最后的结果。子查询一定要重命名。</p>
<figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs pgsql"><span class="hljs-keyword">select</span> count(date2) / count(date1) <span class="hljs-keyword">as</span> avg_ret<br><span class="hljs-keyword">from</span> (<br>    <span class="hljs-keyword">select</span><br>        <span class="hljs-keyword">distinct</span> qpd.device_id,<br>        qpd.date <span class="hljs-keyword">as</span> date1,<br>        uniq_id_date.date <span class="hljs-keyword">as</span> date2<br>    <span class="hljs-keyword">from</span> question_practice_detail <span class="hljs-keyword">as</span> qpd<br>    <span class="hljs-keyword">left join</span>(<br>        <span class="hljs-keyword">select</span> <span class="hljs-keyword">distinct</span> device_id, <span class="hljs-type">date</span><br>        <span class="hljs-keyword">from</span> question_practice_detail<br>    ) <span class="hljs-keyword">as</span> uniq_id_date<br>    <span class="hljs-keyword">on</span> qpd.device_id=uniq_id_date.device_id<br>        <span class="hljs-keyword">and</span> date_add(qpd.date, <span class="hljs-type">interval</span> <span class="hljs-number">1</span> day)=uniq_id_date.date<br>) <span class="hljs-keyword">as</span> id_last_next_date<br></code></pre></td></tr></table></figure>

<p>题目来源</p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/hexo-gujijih/categories/sql/">sql</a>
                    
                      <a class="hover-with-bg" href="/hexo-gujijih/categories/sql/%E5%88%B7%E9%A2%98/">刷题</a>
                    
                  </div>
                
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/hexo-gujijih/2023/02/10/%E3%80%90mysql-%E5%A4%87%E4%BB%BD%E6%81%A2%E5%A4%8D%E3%80%91%E5%AF%BC%E5%85%A5%E5%AF%BC%E5%87%BA%E5%91%BD%E4%BB%A4/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Mysql利用命令导入导出数据-备份和恢复</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/hexo-gujijih/2023/02/10/%E3%80%90mysql-%E6%97%A5%E5%BF%97%E3%80%91%E9%94%99%E8%AF%AF%E6%97%A5%E5%BF%97%E3%80%81%E6%99%AE%E9%80%9A%E6%97%A5%E5%BF%97%E3%80%81%E6%85%A2%E6%9F%A5%E8%AF%A2%E6%97%A5%E5%BF%97/">
                        <span class="hidden-mobile">Mysql错误日志、普通日志、慢查询日志</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments" lazyload>
                
                  
                
                
  <div id="twikoo"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#comments', function() {
      Fluid.utils.createScript('https://cdn.jsdelivr.net/npm/twikoo@1/dist/twikoo.all.min.js', function() {
        var options = Object.assign(
          {"envId":"blog9528-5gm3bi65f33458fb","region":"ap-shanghai","path":"window.location.pathname"},
          {
            el: '#twikoo',
            path: 'window.location.pathname',
            onCommentLoaded: function() {
              Fluid.plugins.initFancyBox('#twikoo .tk-content img:not(.tk-owo-emotion)');
            }
          }
        )
        twikoo.init(options)
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/hexo-gujijih/js/events.js" ></script>
<script  src="/hexo-gujijih/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/hexo-gujijih/js/local-search.js" ></script>



  
    <script  src="/hexo-gujijih/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/hexo-gujijih/js/boot.js" ></script>


</body>
</html>
